/*
* Copyright (C) 2021 ~ 2022 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "usbdevicedetailsdelegate.h"
#include "usbrecordsdatadefinition.h"

#include <QApplication>
#include <QPalette>
#include <QPainter>
#include <QMouseEvent>
#include <QToolTip>
#include <QApplication>

// USB设备详情 表格显示列
#define DETAILS_COLUMN_ADDTIME 0 // 时间列
#define DETAILS_COLUMN_STRATEGY 1 // 限制状态列

#define PIXELSIZE 2

USBDeviceDetailsDelegate::USBDeviceDetailsDelegate(QObject *parent)
    : QStyledItemDelegate(parent)

{
}

// 绘制
void USBDeviceDetailsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem itemOption(option);
    // 设置交替色
    if (index.row() % 2 == 1) {
//        DPalette pl(DApplicationHelper::instance()->palette(option.widget));
        QPalette pl = qApp->palette();
        QPainterPath path;

        if (index.column() == 0) {
            path.setFillRule(Qt::WindingFill);
            path.addRoundedRect(option.rect, 10, 10);
            QRect temp_rect(option.rect.left() + 10, option.rect.top(), option.rect.width() - 10, option.rect.height());
            path.addRect(temp_rect);
            painter->fillPath(path, pl.brush(QPalette::Background));
        } else {
            path.setFillRule(Qt::WindingFill);
            path.addRoundedRect(option.rect, 10, 10);
            QRect temp_rect(option.rect.left(), option.rect.top(), option.rect.width() / 2, option.rect.height());
            path.addRect(temp_rect);
            painter->fillPath(path, pl.brush(QPalette::Background));
        }
    }

    // 第一列 USB设备连接时间
    if (DETAILS_COLUMN_ADDTIME == index.column()) {
        painter->save();
        QString usbAddTime = index.model()->data(index, DATA_ROLE_TIME).toString();
        // 设置字体
        //int currentFontSize = DFontSizeManager::instance()->fontPixelSize(qApp->font());
        int currentFontSize = PIXELSIZE;
        QFont font;
        font.setPixelSize(currentFontSize);
        painter->setFont(font);
        QFontMetrics fontWidth(font);

        QRect textRect = itemOption.rect;
        QPoint textPoint(textRect.x() + 10, textRect.y() + 10);

        usbAddTime = fontWidth.elidedText(usbAddTime, Qt::ElideMiddle, itemOption.rect.width() - 10); // 设置缩略
        painter->drawText(textPoint, usbAddTime);
        painter->restore();
    }

    // 第二列 USB 权限设置
    if (DETAILS_COLUMN_STRATEGY == index.column()) {
        painter->save();
        QString usbStrategy = index.model()->data(index, DATA_ROLE_STRATEGY).toString();
        // 设置字体
//        int currentFontSize = DFontSizeManager::instance()->fontPixelSize(qApp->font());
        int currentFontSize = PIXELSIZE;
        QFont font;
        font.setPixelSize(currentFontSize);
        QRect textRect = itemOption.rect;
        QPoint textPoint(textRect.x() + 10, textRect.y() + 10);
        painter->setFont(font);
        QFontMetrics fontWidth(font);

        usbStrategy = fontWidth.elidedText(usbStrategy, Qt::ElideMiddle, itemOption.rect.width() - 10); // 设置缩略
        painter->drawText(textPoint, usbStrategy); // 绘制文本
        painter->restore();
    }

    if (itemOption.state & QStyle::State_HasFocus) {
        itemOption.state = itemOption.state ^ QStyle::State_HasFocus;
    }

    QStyledItemDelegate::paint(painter, itemOption, index);
}

// 绘制显示tips
bool USBDeviceDetailsDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if (event->type() == QEvent::ToolTip) {
        QString tooltip;
        QStyleOptionViewItem itemOption(option);
        QRect rect = option.rect;

        // 连接时间 第1列
        if (index.column() == DETAILS_COLUMN_ADDTIME) {
            QRect usbNameRect(rect.x() + 10, rect.y() + 10, rect.width(), rect.height());
            // 判断是否缩略
            if (usbNameRect.contains(event->pos())) {
                tooltip = index.data(Qt::DisplayRole).toString();
            } else {
                QWidgetList qwl = QApplication::topLevelWidgets();
                for (QWidget *qw : qwl) {
                    if (QString("QTipLabel") == qw->metaObject()->className()) {
                        qw->close();
                    }
                }
            }
        }

        // 限制状态 第2列
        if (index.column() == DETAILS_COLUMN_STRATEGY) {
            QRect usbTimeRect(rect.x() + 10, rect.y() + 10, rect.width(), rect.height());
            if (usbTimeRect.contains(event->pos())) {
                tooltip = index.data(Qt::DisplayRole).toString();
            } else {
                QWidgetList qwl = QApplication::topLevelWidgets();
                for (QWidget *qw : qwl) {
                    if (QString("QTipLabel") == qw->metaObject()->className()) {
                        qw->close();
                    }
                }
            }
        }

        QToolTip::showText(event->globalPos(), tooltip);
        return true;
    }
    return QStyledItemDelegate::helpEvent(event, view, option, index);
}
